package com.manshutingyu.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.manshutingyu.common.core.dto.DictDTO;
import com.manshutingyu.system.domain.SysDict;
import com.manshutingyu.system.domain.SysDictData;
import com.manshutingyu.system.mapper.SysDictMapper;
import com.manshutingyu.system.service.SysDictDataService;
import com.manshutingyu.system.service.SysDictService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author：yinyuxia
 * @Date：2025/3/29 20:20
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
    @Resource
    private SysDictDataService sysDictDataService;

    @Override
    public List<DictDTO> getAllDictWithData() {
        List<SysDict> dictList = this.list();
        List<SysDictData> dictDataList = sysDictDataService.list();
        if (!CollectionUtils.isEmpty(dictList)) {
            return dictList.stream().map(dict -> {
                DictDTO dictVO = new DictDTO();
                dictVO.setName(dict.getName());
                dictVO.setType(dict.getType());
                dictVO.setData(covertData(dict.getType(), dictDataList));
                return dictVO;
            }).toList();
        }
        return null;
    }

    private List<DictDTO.DictData> covertData(String type, List<SysDictData> dictDataList) {
        if (!CollectionUtils.isEmpty(dictDataList)) {
            return dictDataList.stream().filter(dictData -> type.equals(dictData.getType())).map(dictData -> {
                DictDTO.DictData data = new DictDTO.DictData();
                data.setLabel(dictData.getLabel());
                data.setValue(dictData.getValue());
                data.setTagType(dictData.getListClass());
                return data;
            }).toList();
        }
        return null;
    }
}
